package com.plian.system.service.yth.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.plian.Tools.CollectionUtil;
import com.plian.system.entity.qg.evaluate.EvaluateReform;
import com.plian.system.entity.yth.EvaluationPlan;
import com.plian.system.mapper.yth.EvaluationPlanMapper;
import com.plian.system.service.yth.IEvaluationPlanService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.time.ZoneId;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Optional;

/**
 * @Description: TODO
 * @Author: Take-off
 * @Date: 2023/4/18 3:58 PM
 * @Param
 * @return
 * @Version: 1.0
 **/
@Slf4j
@Service
public class EvaluationPlanServiceImpl extends ServiceImpl<EvaluationPlanMapper,EvaluationPlan> implements IEvaluationPlanService {
    private final String YTH_DATASOURCE_TYPE = "3";

    @Override
//    @DS("slave_1")
    public List<EvaluateReform> getAllProject(Date startTime, Date endTime){
        List<EvaluateReform> evaluateReforms = new ArrayList<>();

        List<EvaluationPlan> allEvaluate = list(new LambdaQueryWrapper<EvaluationPlan>()
                .gt(Optional.ofNullable(startTime).isPresent(),EvaluationPlan::getCreateTime,startTime)
                .le(Optional.ofNullable(endTime).isPresent(),EvaluationPlan::getCreateTime,endTime)
        );
        if (CollectionUtil.isNotEmpty(allEvaluate)){
            evaluateReforms = conversion(allEvaluate);
        }
        return evaluateReforms;
    }

    /**
     * @Description: //TODO 转换
     * @Author: Take-off
     * @Date: 4:09 PM 2023/4/18
     * @Param: [evaluationPlans]
     * @return: java.util.List<com.plian.system.entity.qg.evaluate.EvaluateReform>
     **/
    private List<EvaluateReform> conversion(List<EvaluationPlan> evaluationPlans){
        List<EvaluateReform> evaluateReforms = new ArrayList<>();
        ZoneId zone = ZoneId.systemDefault();
        for (EvaluationPlan evaluationPlan:evaluationPlans){
            EvaluateReform evaluateReform = new EvaluateReform();
            evaluateReform.setEnterpriseNameId(evaluationPlan.getReportOrgId().toString());
            evaluateReform.setIntermediaryOrgan(evaluationPlan.getIntermediaryAgency());
            evaluateReform.setProgress((int)evaluationPlan.getDevelopmentProgress());
            evaluateReform.setReportPerson(evaluationPlan.getReportUserId().toString());
            evaluateReform.setReportTime(Date.from(evaluationPlan.getReportTime().atStartOfDay().atZone(zone).toInstant()) );
            evaluateReform.setDataSourceId(evaluationPlan.getId().toString());
            evaluateReform.setDataSource(YTH_DATASOURCE_TYPE);
            evaluateReform.setDataSourceId(evaluationPlan.getId().toString());
            evaluateReforms.add(evaluateReform);
        }
        return evaluateReforms;
    }

}
